package com.example.ex2.service.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ex2.domain.User;
import com.example.ex2.persistence.UserMapper;
import com.example.ex2.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper UserMapper;

    @Override
    public boolean addUserByUser(User User){
        return (UserMapper.insert(User) > 0);
    }

    @Override
    public boolean deleteUserById(int id){
        return (UserMapper.deleteById(id) > 0);
    }

    @Override
    public List<User> selectAllUsers(){
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        return UserMapper.selectList(queryWrapper);
    }

    @Override
    public boolean updateUserByUser(User User){
        return (UserMapper.updateById(User) > 0);
    }

    @Override
//    int resetUserPassword(int user_id);
    public boolean resetUserPassword(int user_id) {
        // 在实际应用中，这里可能需要生成新的随机密码、加密存储等，这里只是一个简单的示例
        String newPassword = "123456";

        User user = new User();
        user.setUser_id(user_id);
        user.setPassword(newPassword);

        int rowsAffected = UserMapper.updateById(user);
        return rowsAffected > 0;
    }
}
